﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Drawing;

namespace Utility
{
    public abstract class ImagesHelper
    {
        /// <summary>
        /// 输出图片文件的二进制流
        /// </summary>
        /// <param name="ImgPath"></param>
        public static void File2Stream(string ImgPath)
        {
            FileStream stream1 = new FileStream(HttpContext.Current.Server.MapPath(ImgPath), FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] buffer = new BinaryReader(stream1).ReadBytes((int)stream1.Length);
            HttpContext.Current.Response.BinaryWrite(buffer);
        }

        /// <summary>
        /// 输出带有干扰的文字图片
        /// </summary>
        /// <param name="checkCode">要输出的文字</param>
        public static void CreateCheckCodeImage(string checkCode)
        {
            if (checkCode == null || checkCode.Trim() == String.Empty)
                return;

            Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
            Graphics g = Graphics.FromImage(image);

            try
            {
                //生成随机生成器
                Random random = new Random();

                //清空图片背景色
                g.Clear(Color.White);

                //画图片的背景噪音线
                for (int i = 0; i < 25; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);

                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }

                Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(checkCode, font, brush, 2, 2);

                //画图片的前景噪音点
                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);

                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }

                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.ContentType = "image/Gif";
                HttpContext.Current.Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }

        /// <summary>
        /// 生成验证码，样式二
        /// </summary>
        /// <param name="checkCode"></param>
        public static void CreateCheckCodeImage1(string checkCode)
        {
            int iwidth = (int)(checkCode.Length * 16);
            Bitmap image = new Bitmap(iwidth, 22);
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.White);
            //定义颜色
            Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Chocolate, Color.Brown, Color.DarkCyan, Color.Purple };
            //Color[] c = { Color.Black, Color.Red };
            //定义字体            
            string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial" };
            Random rand = new Random();
            //随机输出噪点
            for (int i = 0; i < 50; i++)
            {
                int x = rand.Next(image.Width);
                int y = rand.Next(image.Height);
                g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
            }

            //输出不同字体和颜色的验证码字符
            for (int i = 0; i < checkCode.Length; i++)
            {
                int cindex = rand.Next(7);
                int findex = rand.Next(4);

                Font f = new Font(font[findex], 12, FontStyle.Bold);
                Brush b = new SolidBrush(c[cindex]);
                g.DrawString(checkCode.Substring(i, 1), f, b, 1 + (i * 14), 1);
            }
            //画一个边框
            g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1);

            //输出到浏览器
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ContentType = "image/Jpeg";
            HttpContext.Current.Response.BinaryWrite(ms.ToArray());
            g.Dispose();
            image.Dispose();
        }

        /// <summary>
        /// 简约样式
        /// </summary>
        /// <param name="checkCode"></param>
        public static void CreateCheckCodeImage2(string checkCode)
        {
            int iwidth = (int)(checkCode.Length * 16);
            Bitmap image = new Bitmap(iwidth, 22);
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.White);
            //定义颜色
            //Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Chocolate, Color.Brown, Color.DarkCyan, Color.Purple };
            Color[] c = { Color.Black, Color.Black, Color.DarkBlue };
            //定义字体            
            string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial" };
            Random rand = new Random();

            //输出不同字体和颜色的验证码字符
            for (int i = 0; i < checkCode.Length; i++)
            {
                int cindex = rand.Next(3);
                int findex = rand.Next(4);

                Font f = new Font(font[findex], 11, FontStyle.Bold);
                Brush b = new SolidBrush(c[cindex]);
                g.DrawString(checkCode.Substring(i, 1), f, b, 1 + (i * 14), 1);
            }

            //输出到浏览器
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ContentType = "image/Jpeg";
            HttpContext.Current.Response.BinaryWrite(ms.ToArray());
            g.Dispose();
            image.Dispose();
        }

        /// <summary>
        /// 生成指定长度的随机验证码
        /// </summary>
        /// <param name="codeCount"></param>
        /// <param name="isSaveCookies"></param>
        /// <returns></returns>
        public static string GenerateCheckCode(int codeCount, bool isSaveCookies)
        {
            string checkCode = Common.GenerateRandom(codeCount);
            if (isSaveCookies)
                HttpContext.Current.Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
            else
                HttpContext.Current.Session.Contents["ValidateCode"] = checkCode;
            return checkCode;
        }

        public static string CurrentCheckCode
        {
            get
            {
                return HttpContext.Current.Session.Contents["ValidateCode"].ToString() ?? string.Empty;
            }
        }

    }
}
